﻿namespace Api.Controllers;

/// <summary>
/// 调度控制器
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[AllowAnonymous]
public class JobController(IOperationLogBLL operationLogBLL,IConfiguration configuration, IIdsUserService idsUserService,ISystemConfigBLL systemConfigBLL) : BaseController(operationLogBLL)
{
    private readonly IConfiguration _configuration = configuration;
    private readonly IIdsUserService _idsUserService = idsUserService;
    private readonly ISystemConfigBLL _systemConfigBLL = systemConfigBLL;

    #region 软授权到期前推送消息到统一消息平台
    /// <summary>
    /// 软授权到期前推送消息到统一消息平台
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<ResultModel> SFAuthExpiresReminderSendMessageJob()
    {
        var res = new ResultModel();
        await Task.Run(async () =>
        {
            try
            {
                Log.Write("软授权到期前推送消息到统一消息平台");
                var sfConfigDayStr = _configuration["SFAuthExpiresReminderDayByMessage"] ?? "";
                if (string.IsNullOrWhiteSpace(sfConfigDayStr))
                {
                    res.code = 1;
                    return res;
                }
                sfConfigDayStr = sfConfigDayStr.Replace("，", ",");
                var sfConfigDayList = sfConfigDayStr.Split(",").Select(x => Convert.ToInt32(x)).ToList();

                var pageIndex = 1;
                var pageSize = int.MaxValue;
                var ids_appid = _configuration["IdServer:APPID"];
                var idsUserList = _idsUserService.GetAppUserPageAsync(ids_appid, pageIndex, pageSize).Result;
                if (idsUserList.Data.Rows.Length == 0)
                {
                    res.code = 1;
                    return res;
                }
                var appAdminUser = idsUserList.Data.Rows.FirstOrDefault(x => x.IsAdmin);
                if (appAdminUser == null || appAdminUser.UserId <= 0 || string.IsNullOrWhiteSpace(appAdminUser.Phone))
                {
                    res.code = 1;
                    return res;
                }
                var phone = Util.PhoneDecrypt(appAdminUser.Phone);
                var sf = SoftwareAuthorization.GetAuth();
                var config = _systemConfigBLL.Get(x => x.Key == "system_name");
                var systemName = config.Value;
                foreach (var itsfConfigDay in sfConfigDayList)
                {
                    if (DateTime.Now.Date.AddDays(itsfConfigDay) == DateTime.Parse(sf.expiredate).AddDays(1))
                    {
                        var mqObj = new MessagequeuesReq
                        {
                            Msgtype = MsgTypeEnum.DT_SendMsgByPhone.ToDescription(),
                            ContentObj = new ContentMsg
                            {
                                UserPhones = phone,
                                Content = $"您的{systemName}授权将于[{sf.expiredate}]到期，为保障您正常使用，请联系我们的客户支持团队",
                            }
                        };
                        await AVCUMService.SendMsg(mqObj);


                        mqObj = new MessagequeuesReq
                        {
                            Msgtype = MsgTypeEnum.CORPWX_SendMsgByPhone.ToDescription(),
                            ContentObj = new ContentMsg
                            {
                                UserPhones = phone,
                                Content = $"您的{systemName}授权将于[{sf.expiredate}]到期，为保障您正常使用，请联系我们的客户支持团队",
                            }
                        };
                        await AVCUMService.SendMsg(mqObj);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(ex);
                res.code = -1;
                res.msg = ex.Message;
                return res;
            }
            res.code = 1;
            return res;
        });
        return res;
    }
    #endregion
}
